package com.css.apps.base.user.action;

import com.css.apps.base.user.po.UserInfoDialogVo;
import com.css.apps.base.user.po.UserInfoPageVo;
import com.css.apps.base.user.service.UserService;
import com.css.core.configuration.Environment;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.UuidUtil;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@SuppressWarnings("serial")
public class AddUserInfoPage extends AdminAction{

	private static Log log = LogFactory.getLog(AddUserInfoPage.class);
	private String lableId;
	private String userId;
	private String mode;//用于区分是lable页上的保存 还是dialog上的保存
	private UserInfoPageVo userInfoPageVo;
	private UserInfoDialogVo userInfoDialogVo;

	protected String adminGo() {
		TransactionCache tx = null;
		try {
			String GENERATE_UUID = UuidUtil.getUuid();
			UserService userService = new UserService();
			//解决xwork无法拦截yyyy-MM格式Date数据的问题
			userService.handleDateparam(ServletActionContext.getRequest(),userInfoPageVo);
            switch (mode){
				case "lable":
					//输入前置检查
					if(!userService.perCheck(lableId,userInfoPageVo,"add")){
						result = userService.getResult();
						return Action.ERROR;
					}
					if("userInfo".equals(lableId)){
						this.userId = GENERATE_UUID;
					}
					tx = userService.saveDB(GENERATE_UUID, lableId, userInfoPageVo, tx);
					break;
				case "dialog":
					//输入前置检查
					if(!userService.perCheck(lableId,userInfoDialogVo,"add")){
						result = userService.getResult();
						return Action.ERROR;
					}
					tx = userService.saveDB(GENERATE_UUID, lableId, userInfoDialogVo, tx);
					break;
			}
			JSONObject jsonObject = new JSONObject().element("userId",userId).element("lableId",lableId);
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"),jsonObject);
			return Action.SUCCESS;
		} catch (Exception ex) {
			if (tx != null) {
				tx.rollback();
			}
            result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	public String getLableId() {
		return lableId;
	}

	public void setLableId(String lableId) {
		this.lableId = lableId;
	}

	public UserInfoPageVo getUserInfoPageVo() {
		return userInfoPageVo;
	}

	public void setUserInfoPageVo(UserInfoPageVo userInfoPageVo) {
		this.userInfoPageVo = userInfoPageVo;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public UserInfoDialogVo getUserInfoDialogVo() {
		return userInfoDialogVo;
	}

	public void setUserInfoDialogVo(UserInfoDialogVo userInfoDialogVo) {
		this.userInfoDialogVo = userInfoDialogVo;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
}
